﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GPMS.Web.Controllers
{
    public class ResultController : Controller
    {
        //public ActionResult Success()
        //{
        //    return Success("操作成功");
        //}

        public ActionResult Success(string msg)
        {
            ViewData["msg"] =string.IsNullOrEmpty(msg)? "操作成功": msg;
            return PartialView("Success");
        }

        public ActionResult JsonError(string msg)
        {
            var json = new Dictionary<string, object> { { "error", true }, { "msg", msg } };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [NonAction]
        public static string GetError(ModelStateDictionary modelState)
        {
            var errorMsg = modelState.Keys.Select(key => modelState[key].Errors.FirstOrDefault()).
                Where(error => error != null).Aggregate("", (current, error) => current += (error.ErrorMessage + "\r\n"));
            return errorMsg;
        }
    }
}
